<template>
  <div>
    <el-form v-show="showSearch" ref="queryForm" :model="queryParams" label-width="68px" size="mini">
      <el-row :gutter="20">
        <el-col :span="4">
          <el-form-item label="项目区域" prop="projectRegion">
            <el-select v-model="queryParams.projectRegion" clearable placeholder="请选择"
                       @keyup.enter.native="handleQuery">
              <el-option v-for="dict in dict.type.cld_project_region" :key="dict.value" :label="dict.label"
                         :value="parseInt(dict.value)"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="4">
          <el-form-item label="项目名称" prop="projectName">
            <el-input v-model="queryParams.projectName" clearable placeholder="请输入项目名称"
                      @keyup.enter.native="handleQuery"/>
          </el-form-item>
        </el-col>
        <el-col :span="4">
          <el-form-item label="负责人" prop="headPerson">
            <el-input v-model="queryParams.headPerson" clearable placeholder="请输入负责人"
                      @keyup.enter.native="handleQuery"/>
          </el-form-item>
        </el-col>
        <el-col :span="4">
          <el-form-item label-width="10px">
            <el-button icon="el-icon-search" size="mini" type="primary" @click="handleQuery">搜索</el-button>
            <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button v-hasPermi="['cld:project:add']" icon="el-icon-plus" plain size="mini" type="primary"
                   @click="handleAdd">新建项目
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="projectList" border>
      <el-table-column align="center" label="项目区域" prop="projectRegion" width="120px;">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.cld_project_region" :value="scope.row.projectRegion"/>
        </template>
      </el-table-column>
      <el-table-column align="center" label="项目名称" prop="projectName" width="200px;"/>
      <el-table-column align="center" label="项目业态" prop="projectBusiness" width="100px;">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.cld_project_business" :value="scope.row.projectBusiness"/>
        </template>
      </el-table-column>
      <el-table-column align="center" label="项目类型" prop="projectType" width="100px;">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.cld_project_type" :value="scope.row.projectType"/>
        </template>
      </el-table-column>
      <el-table-column align="center" label="负责人" prop="headPerson" width="100px;"/>
      <el-table-column align="center" label="联系电话" prop="headPhone" width="120px;"/>
      <el-table-column align="center" label="项目报价" prop="yearFee" width="150px;">
        <template slot-scope="scope">
          <span>{{ toMoney(scope.row.yearFee) }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="月利润" prop="monthlyProfit" width="150px;">
        <template slot-scope="scope">
          <span>{{ toMoney(scope.row.monthlyProfit) }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="年利润" prop="annualProfit" width="150px;">
        <template slot-scope="scope">
          <span>{{ toMoney(scope.row.annualProfit) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="项目地址" prop="address" />
      <el-table-column align="center" label="采集人" prop="nickName" width="100px;"/>
      <el-table-column align="center" label="创建人" prop="createName" width="100px;"/>
      <el-table-column align="center" label="创建日期" prop="createTime" width="160px;">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" class-name="small-padding fixed-width" fixed="right" label="操作" width="220px;">
        <template slot-scope="scope">
          <el-button v-hasPermi="['cld:project:signing']" size="mini" type="text"
                     @click="handleContractSigning(scope.row)">合同签订
          </el-button>
          <el-button v-hasPermi="['cld:project:estimate']" size="mini" type="text"
                     @click="handleProfitEstimation(scope.row)">计划设置
          </el-button>
          <el-button v-hasPermi="['cld:project:edit']" size="mini" type="text"
                     @click="handleUpdate(scope.row)">编辑
          </el-button>
          <el-button v-hasPermi="['cld:project:remove']" size="mini" type="text"
                     @click="handleDelete(scope.row)">删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :limit.sync="queryParams.pageSize" :page.sync="queryParams.pageNum" :total="total"
                @pagination="getList"/>
    <!-- 添加或修改项目对话框 -->
    <el-dialog :close-on-click-modal="false" :visible.sync="open" append-to-body title="项目信息" width="50%">
      <add-project-form v-if="open" ref="addProject" :projectId="projectId" @success="handleProjectSuccess"/>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitProjectForm">确 定</el-button>
        <el-button @click="open = false">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 合同签署对话框 -->
    <el-dialog :close-on-click-modal="false" :visible.sync="openSign" append-to-body title="合同签订" width="60%">
      <add-contract v-if="openSign" ref="contract" :projectId="projectId"
                    @success="handleContractSuccess"></add-contract>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitSignForm">确 定</el-button>
        <el-button @click="cancelSign">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import {delProject, listProject} from "@/api/cld/project";
import {getSysUserList} from "@/api/system/user";
import profitEstimation from "./profitEstimation.vue";
import addProjectForm from "./addProject.vue";
import addContract from "./addContract.vue";

export default {
  name: "projectEstablishment",
  dicts: ['cld_project_type', 'cld_project_business', 'cld_project_region'],
  components: {
    profitEstimation,
    addProjectForm,
    addContract
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      projectList: [],
      // 显示搜索条件
      showSearch: true,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        projectRegion: null,
        projectName: null,
        headPerson: null,
        projectStatus: 1
      },
      open: false,
      // 是否显示签订的弹出层
      openSign: false,
      //是否显示计划设置弹出层
      openProfitEstimation: false,
      // 总条数
      total: 0,
      personOptions: [],
      // 项目
      projectId: null
    };
  },
  created() {
    this.getList();
    this.handleGetListSysUser();
  },
  methods: {
    /** 查询项目列表 */
    getList() {
      this.loading = true;
      listProject(this.queryParams).then(response => {
        this.projectList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 获取用户列表
    handleGetListSysUser() {
      getSysUserList().then(res => {
        this.personOptions = res.data;
      })
    },

    /** 新增按钮操作 */
    handleAdd() {
      this.open = true;
      this.projectId = null;
    },
    /** 新增合同签订操作 */
    handleContractSigning(row) {
      this.projectId = row.projectId;
      this.openSign = true;
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.projectId = row.projectId;
      this.open = true;
    },
    // 项目信息保存
    submitProjectForm() {
      this.$refs.addProject.submitForm();
    },
    // 合同信息保存
    submitSignForm() {
      this.$refs.contract.submitSignForm();
    },
    // 项目信息新增修改回调
    handleProjectSuccess() {
      this.open = false;
      this.getList();
    },
    // 合同信息新增回调
    handleContractSuccess() {
      this.openSign = false;
      this.getList();
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },

    /** 计划设置操作 */
    handleProfitEstimation(row) {
      this.$router.push("/project/profitEstimation/profitEstimation/" + row.projectId);
    },
    // 取消签订按钮
    cancelSign() {
      this.openSign = false;
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      this.$modal.confirm('是否确认删除项目名称为"' + row.projectName + '"的数据项？').then(function () {
        return delProject(row.projectId);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
  }
};
</script>
<style scoped>
.el-select {
  width: 100%;
}

.el-date-editor.el-input,
.el-date-editor.el-input__inner {
  width: 100%;
}
</style>
